package controllers;

import java.util.List;

import org.codehaus.jackson.node.ObjectNode;

import com.avaje.ebean.Ebean;

import models.edu_comments;
import models.edu_courses;
import models.edu_coursesstudents;
import models.edu_tags;
import models.edu_users;
import models.edu_instructors;
import models.edu_institutions;
import models.edu_comments;
import play.data.Form;
import play.db.ebean.Model;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Http.Session;
import play.mvc.Result;
import views.html.*;

public class User extends Controller {
	public static Result login(){
    	return ok(login.render());
    }
    
	public static Result register(){
    	return ok(register.render());
    }
    
	public static Result registerToEdunera(){
		
		edu_users eu=Form.form(edu_users.class).bindFromRequest().get();
		eu.save();
		return redirect(routes.User.register());
	}
	
	public static Result activateUser(String Id)
	{
		boolean updated = edu_users.changeActiveStatusById(Integer.parseInt(Id),true);
		return redirect(routes.AdminOperations.Admin());
	}
	
	public static Result deactivateUser(String Id)
	{
		boolean updated = edu_users.changeActiveStatusById(Integer.parseInt(Id),false);
		return redirect(routes.AdminOperations.Admin());
	}	
	
	public static Result updateRegistration(){
    	return ok(registrationupdate.render());
    }
	
	public static Result returnJson()	
	{
		List<edu_users> l=new Model.Finder(String.class,edu_users.class).all();
		
		return ok(Json.toJson(l));
	}
	
	public static Result getUserByID(String Id)
	{
		edu_users eu=edu_users.findById(Integer.parseInt(Id));
		
		return ok(Json.toJson(eu));
	}
	
	public static Result getUserByEmail(String Email){
		edu_users eu=edu_users.findByEmail(Email);
		
		return ok(Json.toJson(eu));
	}

	public static Result ajaxlogin(String email,String password)
	{
		ObjectNode on=Json.newObject();
		
		edu_users eu = edu_users.validateUser(email, password);
		
		if(session().containsKey("email") && session().get("email").equals(email)){
			on.put("status", "already");
		}
		else if(eu.user_id > 0){
			session().put("email", email);
			session().put("user_id", Integer.toString(eu.user_id));
			on.put("status", "successfull");
		}
		else
			on.put("status", "failed");
		return ok(Json.toJson(on));
	}

	public static Result logout()
	{
		session().clear();
		return redirect(routes.Application.index());
	}
	
	
	public static Result addCourse()
	{
		return ok(addnewcourse.render());
	}
	
	//Mobile
	
	public static Result LoginWithAndroidDevice(String user_device_id){
		ObjectNode on=Json.newObject();
		if(session().containsKey("email")){
			on.put("status", "already");
		}
		else if(edu_users.getUserByDeviceId(user_device_id)){
			on.put("status", "successfull");
		}
		else
			on.put("status", "failed");
		return ok(Json.toJson(on));
	}

	public static Result mobileLogin(String email,String password,String user_device_id)
	{
		ObjectNode on=Json.newObject();
		if(session().containsKey("email") && session().get("email").equals(email)){
			on.put("status", "already");
		}
		else if(edu_users.validateUserForMobile(email, password, user_device_id)){
			session().put("email", email);
			on.put("status", "successfull");
		}
		else
			on.put("status", "failed");
		return ok(Json.toJson(on));
	}

	public static Result userHome(){
    	return ok(userhome.render());
    }
	
	public static Result getMyCourses(String userId){
		String s="SELECT edu_coursesstudents.course_id, " +
				"edu_courses.name, " +
				"edu_courses.url, "+
				"edu_instructors.name as instructor_name," +
				"edu_institutions.name as institution_name, " +
				"AVG(IFNULL(edu_courserates.course_rate,0)) as course_rate "+
				"FROM edu_coursesstudents " +
				"LEFT Join edu_courses " +
					"on edu_coursesstudents.course_id=edu_courses.course_id " +
				"LEFT Join edu_instructors " +
					"on edu_courses.instructor_id = edu_instructors.id " +
				"LEFT Join edu_institutions " +
					"on  edu_courses.institution_id = edu_institutions.id " +
				"LEFT Join edu_users " +
					"on edu_coursesstudents.user_id=edu_users.user_id " +
				"LEFT Join edu_courserates " +
					"on edu_courserates.course_id=edu_users.user_id " +
				"Where edu_users.user_id='"+userId+"' AND edu_courses.parent_id=0 "+
				"group by edu_coursesstudents.course_id," +
				"edu_courses.name," +
				"edu_courses.url," +
				"edu_instructors.name," +
				"edu_institutions.name";
		return ok(Json.toJson(Ebean.createSqlQuery(s).findList().toArray()));
	}
	
	public static Result isEnrolledCourse(String courseId){
		String query = "SELECT edu_coursesstudents.course_id "+
				"FROM edu_coursesstudents " +
				"Join edu_courses " +
					"on edu_coursesstudents.course_id=edu_courses.course_id "+
				"Where edu_coursesstudents.user_id='"+session().get("user_id")+"' " +
					"and edu_courses.course_id = "+courseId;
		return ok(Json.toJson(Ebean.createSqlQuery(query).findList().size() > 0));
	}
	
}
